package com.example.drss.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.drss.DRSSActivity;
import com.example.drss.R;
import com.example.drss.SellerActivity;
import com.example.drss.adapter.ListViewAllSellerAdapter;
import com.example.drss.bean.Menu;
import com.example.drss.itemdata.ItemData;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class TwoFragment extends Fragment {
	
	ListView listView;
	List<ItemData> data ;
	DRSSActivity activity;
	
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.fragment_two, container, false);
		activity=(DRSSActivity)getActivity();
		listView=(ListView)view.findViewById(R.id.listView_all_seller);
		data = new ArrayList<ItemData>();  	
    	Cursor cursor=activity.getMenuSeller();
		if (cursor!=null) {
			//读取所有纪录
			List<Menu> menus =new ArrayList<Menu>();
			while(cursor.moveToNext()){
				Menu menu=new Menu();
				menu.setSeller(cursor.getString(0));
				menus.add(menu);
			}
			for(Menu menu:menus){
				ItemData data1= new ItemData();
				data1.setSeller(menu.getSeller());
				data.add(data1);
			}		
		}
		ListViewAllSellerAdapter adapter = new ListViewAllSellerAdapter(activity, data);
    	listView.setAdapter(adapter);
    	
    	listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(activity,SellerActivity.class);
				ItemData itemData=data.get(position);
				intent.putExtra("databasePath_seller", activity.databasePath);
				intent.putExtra("seller_name", itemData.getSeller());
				startActivity(intent);
			}
		});
		return view;
	}
}
